/**
 *
 * Copyright 2015 RSVP Technologies Inc. All rights reserved.
 * StatsSearch.java
 *
 */
package com.gfinal.map.bus;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.DOMReader;
import org.dom4j.io.SAXReader;

import com.gfinal.map.common.Constant;
import com.gfinal.map.util.HttpUtils;

/**
 * @author He Tao (ht@nbicc.com)
 * @date 2015年3月2日
 */
public class StatsSearch {
	public static final String TAG = StatsSearch.class.getName();
	private String city = null;
	private String q = null;
	private int result_num = 0;
	private String web_url = "http://bus.aibang.com";
	private String wap_url = "http://wap.aibang.com";
	private ArrayList<StatBean> lineList;
	
	
	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getQ() {
		return q;
	}

	public void setQ(String q) {
		this.q = q;
	}

	public int getResult_num() {
		return result_num;
	}

	public String getWeb_url() {
		return web_url;
	}

	public String getWap_url() {
		return wap_url;
	}
	
	@SuppressWarnings({ "unused", "rawtypes", "unchecked" })
	private String createUrl() {
		ArrayList params = new ArrayList();
		params.add(new BasicNameValuePair("app_key", Constant.AIBANG_AK));
		params.add(new BasicNameValuePair("city", city));
		params.add(new BasicNameValuePair("q", q));
		return Constant.BUS_STAT_URL + "?"
				+ URLEncodedUtils.format(params, Constant.CHARSET);
	}
	
	@SuppressWarnings("rawtypes")
	public ArrayList<StatBean> execute() {
		lineList = new ArrayList<StatBean>();
		String response = HttpUtils.doGet(createUrl());
		SAXReader reader = new SAXReader();

		Document doc;
		try {
			ByteArrayInputStream in = new ByteArrayInputStream(
					response.getBytes("UTF-8"));
			doc = reader.read(in);
			Element root = doc.getRootElement(); // 获取根节点
			List<Element> list = new ArrayList<Element>();
			list.add(root);
			while (list.size()>0) {
				Element element = null;
				Element ele = null;
				Iterator ite = list.iterator();
				if (ite.hasNext()) {
					ele = (Element) ite.next();
					ite.remove();
				}
				if (ele != null) {
					StatBean statBean = null;
					for (Iterator i = ele.elementIterator(); (i != null)
							&& (i.hasNext());) {
						element = (Element) i.next();
						list.add(element);
						if (element != null) {
							 if(element.getName().equals("result_num")){
								 result_num=Integer.valueOf(element.getText());
							 }
							 else if (element.getName().equals("name")) {
								 statBean = new StatBean();
								 statBean.setName(element.getText());
							} else if (element.getName().equals("xy")) {
								statBean.setXy(element.getText());
							} else if (element.getName().equals("line_names")) {
								statBean.setLine_names(element.getText());
								lineList.add(statBean);
							}
						}
					}
				}
				
				if(lineList.size()==result_num){
					break;
				}
			
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return lineList;

	}
	
	public static void main(String[] args) {
		StatsSearch statsSearch=new StatsSearch();
		statsSearch.setCity("宁波");
		statsSearch.setQ("溪口");
		statsSearch.execute();
	}

}
